﻿namespace Service.Base
{
    public class RedisService : IRedisService
    {
        private string _TenantKey;
        private readonly IBaseRedisService _Service;

        public RedisService(IBaseRedisService service)
        {
            _Service = service;
        }

        public void SetTenantKey(string tenantKey)
        {
            _TenantKey = tenantKey;
        }

        #region SysAtrribute

        private string GetRedisAttrKey(string tenantKey, Enum_RedisKey redisKey, string code)
        {
            return string.Format("{0}:{1}:{2}", tenantKey, redisKey.GetDescription(), code);
        }

        private string GetRedisKey(string[] array)
        {
            return string.Format("{0}:{1}:{2}", array[0], array[1], array[2]);
        }

        public bool SetRedisAttr<T>(string key, T value, Enum_RedisKey redisKey)
        {
            return _Service.Set<T>(GetRedisAttrKey(_TenantKey, redisKey, key), value);
        }

        public bool SetRedisAttr<T>(string tenantKey, string key, T value, Enum_RedisKey redisKey)
        {
            return _Service.Set<T>(GetRedisAttrKey(tenantKey, redisKey, key), value);
        }

        public bool SetRedisAttr<T>(string key, T value, DateTime expiresAt, Enum_RedisKey redisKey)
        {
            return _Service.Set<T>(GetRedisAttrKey(_TenantKey, redisKey, key), value, expiresAt);
        }

        public bool SetRedisAttr<T>(string tenantKey, string key, T value, DateTime expiresAt, Enum_RedisKey redisKey)
        {
            return _Service.Set<T>(GetRedisAttrKey(tenantKey, redisKey, key), value, expiresAt);
        }

        public bool SetRedisAttr<T>(string key, T value, TimeSpan expiresIn, Enum_RedisKey redisKey)
        {
            return _Service.Set<T>(GetRedisAttrKey(_TenantKey, redisKey, key), value, expiresIn);
        }

        public bool SetRedisAttr<T>(string tenantKey, string key, T value, TimeSpan expiresIn, Enum_RedisKey redisKey)
        {
            return _Service.Set<T>(GetRedisAttrKey(tenantKey, redisKey, key), value, expiresIn);
        }

        public T GetRedisAttr<T>(string key, Enum_RedisKey redisKey)
        {
            return _Service.Get<T>(GetRedisAttrKey(_TenantKey, redisKey, key));
        }

        public T GetRedisAttr<T>(string tenantKey, string key, Enum_RedisKey redisKey)
        {
            return _Service.Get<T>(GetRedisAttrKey(tenantKey, redisKey, key));
        }

        //public TSysattribute GetSysAttr(string key)
        //{
        //    return _Service.Get<TSysattribute>(GetRedisAttrKey(_TenantKey, Enum_RedisKey.SysAttribution, key));
        //}

        //public TSysattribute GetSysAttr(string tenantKey, string key)
        //{
        //    return _Service.Get<TSysattribute>(GetRedisAttrKey(tenantKey, Enum_RedisKey.SysAttribution, key));
        //}

        public string GetI18nValue(string key)
        {
            return _Service.Get<string>(GetRedisAttrKey(_TenantKey, Enum_RedisKey.SysAttribution, key));
        }

        public string GetI18nValue(string tenantKey, string key)
        {
            return _Service.Get<string>(GetRedisAttrKey(tenantKey, Enum_RedisKey.SysAttribution, key));
        }

        public bool CheckAdminToken(string token)
        {
            return _Service.ContainsKey(GetRedisAttrKey(_TenantKey, Enum_RedisKey.AdminToken, token));
        }

        public bool CheckAdminToken(string tenantKey, string token)
        {
            return _Service.ContainsKey(GetRedisAttrKey(tenantKey, Enum_RedisKey.AdminToken, token));
        }

        public bool DeleteRedisAttr(string key, Enum_RedisKey redisKey)
        {
            return _Service.Remove(GetRedisAttrKey(_TenantKey, redisKey, key));
        }

        public bool DeleteRedisAttr(string tenantKey, string key, Enum_RedisKey redisKey)
        {
            return _Service.Remove(GetRedisAttrKey(tenantKey, redisKey, key));
        }

        //public string GetSysAttrValue(string key)
        //{
        //    var result = GetSysAttr(key);
        //    if (result != null)
        //    {
        //        return result.AttributeValue;
        //    }
        //    else
        //    {
        //        return string.Empty;
        //    }
        //}

        public bool CheckKey(string key, Enum_RedisKey redisKey)
        {
            return _Service.ContainsKey(GetRedisAttrKey(_TenantKey, redisKey, key));
        }

        public bool CheckKey(string tenantKey, string key, Enum_RedisKey redisKey)
        {
            return _Service.ContainsKey(GetRedisAttrKey(tenantKey, redisKey, key));
        }

        #endregion SysAtrribute
    }
}